package com.easy.util;

import cn.hutool.core.img.ImgUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import com.easy.bean.User;
import com.mysql.cj.QueryResult;
import org.springframework.util.DigestUtils;

import javax.servlet.http.HttpSession;

public class SYS {
    //文件上传保存路径
    public static final String FILE_UPLOAD_DIR = "D:\\easyupload\\";
    //上传文件访问前置路径
    public static final String FILE_UPLOAD_URL = "easyfile";

    public static final String SHOPPING_LOGIN_USER = "SHOPPING_LOGIN_USER";

    public static final String SYSTEM_LOGIN_ADMIN = "SYSTEM_LOGIN_ADMIN";

    public static final int ADDRESS_MAX_NUMBER = 6;

    public static int getLoginUserID(HttpSession session) {
        User user=(User)session.getAttribute(SHOPPING_LOGIN_USER);
        return user.getId();
    }

    public static final int[] HEX_ChAR_ARR = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

    public static  boolean check(String reg, String... params) {
        boolean result = true;
        for (String item : params) {
            if (item == null) {
                result = false;
            }
            if (!item.matches(reg)) {
                result = false;
            }

        }
        return result;
    }

    public static String degest(String pass) {
        pass="123123";
        //随机次数 随机盐值加密
        int countIndex = (int) (Math.random() * HEX_ChAR_ARR.length);
        int saltIndex = (int) (Math.random() * HEX_ChAR_ARR.length);
        int count = HEX_ChAR_ARR[countIndex];
        char salt = (char) HEX_ChAR_ARR[saltIndex];
        char countChar = (char) count;

        pass = pass + salt;

        for (int i = 0; i < count; i++) {
            pass = DigestUtils.md5DigestAsHex(pass.getBytes());
        }
//        System.out.println(pass);
//        System.out.println("盐值:"+salt);
//        System.out.println("次数:"+count);

        String passwordText = salt + pass + countChar;
        return passwordText;
    }

    //登录验证
    public static boolean checkPass(String pass,String hexPass) {
        char salt=hexPass.charAt(0);
        char countChar=hexPass.charAt(hexPass.length()-1);
        pass = pass + salt;
        for (int i = 0; i < countChar; i++) {
            pass=DigestUtils.md5DigestAsHex(pass.getBytes());
        }
        String text=salt+pass+countChar;
        return text.equals(pass);
    }

//    public static void main(String[] args) {
////        QrCodeUtil.generate();
//        ImgUtil.toBase64()
//    }
}
